package com.boot.demofile;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.context.ConfigurableApplicationContext;

import java.io.*;
import java.net.InetAddress;
import java.net.UnknownHostException;

@Slf4j
@SpringBootApplication
public class DemoFileApplication {
 	public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(DemoFileApplication.class, args);
        try {
            String host = InetAddress.getLocalHost().getHostAddress();
            TomcatServletWebServerFactory tomcatServletWebServerFactory= (TomcatServletWebServerFactory) context.getBean(TomcatServletWebServerFactory.class);
            int port = tomcatServletWebServerFactory.getPort();
            String contextPath = tomcatServletWebServerFactory.getContextPath();
            log.info("你的链接是:{}{}:{}","http://",host,port+contextPath);
            DemoFileApplication.createHttp("http://"+host+":"+port+contextPath);
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
	}


	static void createHttp(String url){
        FileOutputStream fos = null;
 	    String path = "D://";
        File file = new File(path);
        if(!file.exists()){
            file.mkdirs();
        }
        try {
           fos = new FileOutputStream(path+"readme.txt");
            fos.write(url.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
